package com.eduhzy.ttw.clazz.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eduhzy.ttw.clazz.R;
import com.eduhzy.ttw.clazz.R2;
import com.eduhzy.ttw.clazz.di.component.DaggerClazzReviewComponent;
import com.eduhzy.ttw.clazz.di.module.ClazzReviewModule;
import com.eduhzy.ttw.clazz.mvp.contract.ClazzReviewContract;
import com.eduhzy.ttw.clazz.mvp.model.entity.ClazzScoreData;
import com.eduhzy.ttw.clazz.mvp.model.entity.HomeData;
import com.eduhzy.ttw.clazz.mvp.presenter.ClazzReviewPresenter;
import com.eduhzy.ttw.clazz.mvp.ui.fragment.ClazzReviewFragment;
import com.eduhzy.ttw.clazz.mvp.ui.fragment.GroupReviewFragment;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.adapter.HomeFragmentAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.umeng.analytics.MobclickAgent;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zyyoona7.popup.EasyPopup;
import com.zyyoona7.popup.XGravity;
import com.zyyoona7.popup.YGravity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-4 11:15:26
 */
@Route(path = RouterHub.CLAZZ_CLAZZREVIEWACTIVITY)
public class ClazzReviewActivity extends BaseActivity<ClazzReviewPresenter> implements ClazzReviewContract.View {

    @Autowired(name = Constants.PARCELABLE_DATA)
    HomeData mBean;


    @BindView(R2.id.tabs)
    TabLayout tabs;
    @BindView(R2.id.viewpager)
    ViewPager viewpager;

    List<Fragment> mFragments;

    String[] mTitles = new String[]{
            "学生", "小组"
    };
    @BindView(R2.id.public_toolbar_right_iv_layout)
    AutoRelativeLayout mIvRightIvLayout;
    @BindView(R2.id.public_toolbar_iv_right)
    ImageView mIvRight;
    private ViewHolder vh;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerClazzReviewComponent
                .builder()
                .appComponent(appComponent)
                .clazzReviewModule(new ClazzReviewModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        MobclickAgent.onEvent(getActivity(),"ttw_class_review_detail");
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_clazz_review;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mIvRightIvLayout.setVisibility(View.VISIBLE);
        mIvRight.setImageResource(R.drawable.icn_gengduo);
        mFragments = new ArrayList<>();
        setDivider();
        ClazzReviewFragment student = ClazzReviewFragment.newInstance(mBean);
        mFragments.add(student);
        GroupReviewFragment group = GroupReviewFragment.newInstance(mBean);
        mFragments.add(group);
        HomeFragmentAdapter adapter = new HomeFragmentAdapter(getSupportFragmentManager(), Arrays.asList(mTitles), mFragments);


        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(mTitles.length);
        tabs.setupWithViewPager(viewpager);
    }

    private void setDivider() {
        /*TabLayout中间分界线*/
        LinearLayout linearLayout = (LinearLayout) tabs.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,
                R.drawable.layout_divider_vertical));
        linearLayout.setDividerPadding(RxUtil.getAutoHeight(getActivity(), 20));
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void updateDialog(List<ClazzScoreData> data) {

    }

    @Override
    public void showDialogLoading(boolean show) {

    }

    private EasyPopup addPop;

    @OnClick(R2.id.public_toolbar_right_iv_layout)
    public void onViewClicked(View view) {
        initAddPop();
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) ((ViewGroup) view).getChildAt(0).getLayoutParams();
        int rightMargin = layoutParams.rightMargin;
        addPop.showAtAnchorView(view, YGravity.BELOW, XGravity.ALIGN_RIGHT, -rightMargin, -rightMargin);

    }

    private void initAddPop() {
        if (addPop == null) {
            addPop = EasyPopup.create()
                    .setContext(this)
                    .setContentView(R.layout.clazz_popup_layout_more)
                    .setOnViewListener((view, easyPopup) -> {
                        if (vh == null) {
                            vh = new ViewHolder(view);
                        }
                    })
//                .setAnimationStyle(R.style.RightTop2PopAnim)
                    //是否允许点击PopupWindow之外的地方消失
                    .setFocusAndOutsideEnable(true)
                    //允许背景变暗
                    .setBackgroundDimEnable(true)
                    //变暗的透明度(0-1)，0为完全透明
                    .setDimValue(0.5f)
                    //变暗的背景颜色
//                .setDimColor(Color.RED)
                    //指定任意 ViewGroup 背景变暗
//                .setDimView(mTitleBar)
                    .apply();
        }
    }

    class ViewHolder {
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        @OnClick({R2.id.btn_report, R2.id.btn_review})
        public void onPopupClicked(View view) {
            addPop.dismiss();
            int i = view.getId();
            if (i == R.id.btn_report) {

                ARouter.getInstance().build(RouterHub.CLAZZ_CLAZZREPORTACTIVITY)
                        .withString(Intent.EXTRA_TITLE, mBean.getClassName())
                        .withParcelable(Constants.PARCELABLE_DATA, mBean)
                        .navigation(getActivity());
            } else if (i == R.id.btn_review) {
                ARouter.getInstance().build(RouterHub.CLAZZ_EDITREVIEWACTIVITY)
                        .withString(Intent.EXTRA_TITLE, "编辑点评")
                        .withParcelable(Constants.PARCELABLE_DATA, mBean)
                        .navigation(getActivity());
            }
        }
    }

}
